package com.atguigu.gmall.pms.vo;

import com.atguigu.gmall.pms.entity.SpuAttrValueEntity;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;


@Setter
public class SpuAttrValueVo extends SpuAttrValueEntity {

    private String[] valueSelected;

    /**
     * 封装为 attrValue,从json读取数据时候，该方法自动自运行
     */
    public void setValueSelected() {
        if (ArrayUtils.isNotEmpty(valueSelected)) {
        /*//拼接数组元素为字符串
        StringBuffer buffer = new StringBuffer();
            for (String s : valueSelected) {
                buffer.append(s).append(",");
            }
            String attrValue = buffer.substring(0, buffer.length() - 1);*/
            //以上代码可用一句完成
            String attrValue = StringUtils.join(valueSelected, ",");//如果数组中元素超过两个，则用逗号拼接
            this.setAttrValue(attrValue);
        }
    }

}
